package com.fulu.netty.luban.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Demo4 {
    public static void main(String[] args) throws IOException {
        FileChannel channelRead = new FileInputStream("demo3.txt").getChannel();
        FileChannel channelWrite = new FileOutputStream("demo4.txt").getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(100);
        while (true){
            // clear 方法将buffer指针初始化，position=0; limit=capacity;
            // 如果不执行clear，read永远读到的是0
            buffer.clear();
            int read = channelRead.read(buffer);
            System.out.println(read);
            if(read == -1){
                break;
            }
            buffer.flip();
            channelWrite.write(buffer);
        }
    }
}
